package com.gxc.controller;

import com.gxc.entity.User;
import com.gxc.service.UserService;
import com.gxc.utils.RestControllerResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author GongXincheng
 * @since 2019-07-30 15:54
 */
@RestController
@RequestMapping("/test")
public class UserController {

    @Resource private UserService userService;

    /** 添加测试数据. */
    @GetMapping("/addEg")
    public RestControllerResult<List<User>> addEg() {
        List<User> userList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setUsername("username_" + i);
            user.setPassword("pwd_" + i);
            user.setBirthday(new Date());
            userList.add(user);
        }
        List<User> result = userService.addEg(userList);
        return RestControllerResult.success(result);
    }

    /** 根据id查询，使用Redis */
    @GetMapping("/search/{id}")
    public RestControllerResult<User> findById(@PathVariable Integer id) {
        User user = userService.findById(id);
        if (user == null) {
            return new RestControllerResult<>(false, "用户不存在");
        }
        return RestControllerResult.success(user);
    }

    @PostMapping("/add")
    public RestControllerResult<User> add(@RequestBody User user) {
        User result = userService.addUser(user);
        return RestControllerResult.success(result);
    }
}
